/**
 * Created by liubin on 2017/5/2.
 */


var DzpkOprationLayer = GameCCSBase.extend({

    panel_opration : null,
    panel_idle : null,

    //空间时选择的快捷操作
    idleSelectedOpration : null,

    //跟注
    Button_call : null,
    //让牌
    Button_check : null,
    //弃牌
    Button_fold : null,
    //加注
    Button_raise : null,
    //all in
    Button_all_in : null,

    //
    Panel_bet : null,

    betChipMinNum : null,
    betChipMaxNum : null,
    betChipNum : null,

    callChipNum : null,
    allInChipNum : null,

    isRoundOfFirst : null,

    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/DzpkOprationLayer.json");

        this.betChipMaxNum = 100;
        this.betChipMinNum = 20;
        this.isRoundOfFirst = false;

        this.loadUI();
    },

    reset : function () {
        this.idleSelectedOpration = 0;
    },

    loadUI : function () {

        this.panel_opration = mo.seekWidgetByName(this.ccsNode, "Panel_opration");
        this.panel_idle = mo.seekWidgetByName(this.ccsNode, "Panel_idle");

        this.Panel_bet = mo.seekWidgetByName(this.ccsNode, "Panel_bet");
        this.Panel_bet.setVisible(false);

        var pos_opration = this.panel_opration.getPosition();
        var pos_idle = this.panel_idle.getPosition();

        this.panel_opration.setPosition(cc.p(pos_opration.x, pos_opration.y - 120));
        this.panel_idle.setPosition(cc.p(pos_idle.x, pos_idle.y - 120));

        //选择加注操作
        this.initRaiseOpration();

        //下注操作
        this.initSelectAction();

        //空间时选择快捷操作
        this.initSelectIdleOPration();

        //选择加注数量
        this.initSelectBetChipNum();
    },

    initRaiseOpration : function () {
        this.Panel_bet.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.Panel_bet.setVisible(false);
                this.setOprationState(true);
            }
        }, this);

        var Button_num = mo.seekWidgetByName(this.Panel_bet, "Button_num");
        Button_num.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.rasieOpration();
            }
        }, this);

        // var betNums = [100, 250, 500, 1000, 2500];
        for (var i=0; i<5; i++){
            var Panel_rasie = mo.seekWidgetByName(this.Panel_bet, "Panel_rasie_" + (i+1));
            var Button_raise =  mo.seekWidgetByName(Panel_rasie, "Button_raise");
            Button_raise.setTag(i);

            Button_raise.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var seatuser = mo.dzpkTableStatus.mTableStatus["SeatUser" + mo.dzpkTableStatus.mTableStatus.MySeat];
                    var maxChipNum = seatuser.CM; //自已拥有的最大筹码金额

                    var betNum = this.betChipMinNum * (sender.getTag() + 2);
                    if(betNum > maxChipNum || betNum < this.betChipMinNum){
                        return;
                    }

                    this.betChipNum = betNum;
                    this.rasieOpration();
                }
            }, this);
        }
    },


    rasieOpration : function () {
        //发送加注操作
        var seatuser = mo.dzpkTableStatus.mTableStatus["SeatUser" + mo.dzpkTableStatus.mTableStatus.MySeat];
        var maxChipNum = seatuser.CM; //自已拥有的最大筹码金额

        if(this.betChipNum == maxChipNum){
            //all in
            mo.gameMsgManager.sendActionRequestdzpk(5, 0, 0);
        }else{
            if(this.betChipNum == this.callChipNum){
                //跟注
                mo.gameMsgManager.sendActionRequestdzpk(1, this.betChipNum, 0);
            }else{
                if(this.isRoundOfFirst){
                    //押注
                    mo.gameMsgManager.sendActionRequestdzpk(0, this.betChipNum, 0);
                }else{
                    //加注
                    mo.gameMsgManager.sendActionRequestdzpk(2, this.betChipNum, 0);
                }
            }
        }

        this.Panel_bet.setVisible(false);
    },


    initSelectAction : function () {
        //加注
        this.Button_raise = mo.seekWidgetByName(this.panel_opration, "Button_raise");
        this.Button_raise.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {

                this.Panel_bet.setVisible(true);

                var seatuser = mo.dzpkTableStatus.mTableStatus["SeatUser" + mo.dzpkTableStatus.mTableStatus.MySeat];
                var maxChipNum = seatuser.CM; //自已拥有的最大筹码金额
                // var betNums = [100, 250, 500, 1000, 2500];


                for (var i=0; i<5; i++){
                    var Panel_rasie = mo.seekWidgetByName(this.Panel_bet, "Panel_rasie_" + (i+1));
                    var Button_raise =  mo.seekWidgetByName(Panel_rasie, "Button_raise");
                    var Text_1 = mo.seekWidgetByName(Panel_rasie, "Text_1");

                    var betNum = this.betChipMinNum * (i + 2);
                    mo.gameHelper.setTextString(Text_1, betNum);

                    if(betNum > maxChipNum || betNum < this.betChipMinNum){
                        Button_raise.setColor(cc.color(100,100,100));
                    }else{
                        Button_raise.setColor(cc.color(255,255,255));
                    }
                }

                this.panel_opration.stopAllActions();
                var opration_toPos = cc.p(this.panel_opration.getPositionX(), -120)
                this.panel_opration.runAction(cc.moveTo(0.2, opration_toPos));

            }
        }, this);

        this.Button_call = mo.seekWidgetByName(this.panel_opration, "Button_call");
        this.Button_call.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                //发送跟注操作
                mo.gameMsgManager.sendActionRequestdzpk(1, this.callChipNum, 0);
            }
        }, this);

        this.Button_fold = mo.seekWidgetByName(this.panel_opration, "Button_fold");
        this.Button_fold.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                //发送弃牌操作
                mo.gameMsgManager.sendActionRequestdzpk(4, 0, 0);
            }
        }, this);

        this.Button_check = mo.seekWidgetByName(this.panel_opration, "Button_chek");
        this.Button_check.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                //发送让牌操作
                mo.gameMsgManager.sendActionRequestdzpk(3, 0, 0);
            }
        }, this);

        this.Button_all_in = mo.seekWidgetByName(this.panel_opration, "Button_all_in");
        this.Button_all_in.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                //发送all in操作
                mo.gameMsgManager.sendActionRequestdzpk(5, 0, 0);
            }
        }, this);
    },

    initSelectIdleOPration : function () {
        this.idleSelectedOpration = 0;
        for (var i=1; i<=4; i++){
            var idleCheckButton = mo.seekWidgetByName(this.panel_idle, "CheckBox_" + i);
            idleCheckButton.setTag(i);
            idleCheckButton.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var index = sender.getTag();

                    if(this.idleSelectedOpration == index){
                        this.idleSelectedOpration = 0;
                    }else{
                        this.idleSelectedOpration = index;
                    }

                    for (var j=1; j<=4; j++) {
                        var idleCheckButton = mo.seekWidgetByName(this.panel_idle, "CheckBox_" + j);
                        var Image_selected = mo.seekWidgetByName(idleCheckButton, "Image_selected");
                        var Image_bg = mo.seekWidgetByName(idleCheckButton, "Image_bg");
                        if(j != this.idleSelectedOpration){
                            Image_selected.setVisible(false);
                            Image_bg.setVisible(false);
                        }else{
                            Image_selected.setVisible(true);
                            Image_bg.setVisible(true);
                        }
                    }
                }
            }, this);
        }
    },


    initSelectBetChipNum : function () {
        var Slider_bet = mo.seekWidgetByName(this.Panel_bet, "Slider_raise");
        Slider_bet.addEventListener(function (sender, type) {
            var percent = Slider_bet.getPercent();
            this.betChipNum = this.percentToChipNum(percent);
            this.updateChipNum();
        }, this);
    },


    chipNumToPercent : function (percent) {
        //如果没有加注，那么加注的金额就是跟注或者all in的金额
        if(this.betChipMaxNum == 0 && this.betChipMinNum == 0){
            return percent;
        }

        if(this.betChipMaxNum == this.betChipMinNum){
            return percent;
        }

        return parseInt((this.betChipNum - this.betChipMinNum) / (this.betChipMaxNum - this.betChipMinNum) * 100);
    },

    percentToChipNum : function (percent) {
        return parseInt(percent/100 * (this.betChipMaxNum - this.betChipMinNum) + this.betChipMinNum);
    },

    //设置加注筹码数量
    setChipNum : function () {
        var Slider_bet = mo.seekWidgetByName(this.Panel_bet, "Slider_raise");
        Slider_bet.setPercent(this.chipNumToPercent(Slider_bet.getPercent()));
        this.updateChipNum();
    },

    //更新加注筹码数量
    updateChipNum : function () {
        var Text_bet = mo.seekWidgetByName(this.Panel_bet, "Text_bet");
        mo.gameHelper.setTextString(Text_bet, this.betChipNum);
    },

    showOperating : function (isMyOperating, myUseActionRequest) {
        var seatuser = mo.dzpkTableStatus.mTableStatus["SeatUser" + mo.dzpkTableStatus.mTableStatus.MySeat];
        var maxChipNum = seatuser.CM; //自已拥有的最大筹码金额

        this.betChipMinNum = 0;
        this.betChipMaxNum = 0;
        this.betChipNum = 0;
        this.callChipNum = 0;
        this.allInChipNum = maxChipNum;
        this.isRoundOfFirst = false;

        for (var j=1; j<=4; j++) {
            if(this.idleSelectedOpration == j){
                continue;
            }
            var idleCheckButton = mo.seekWidgetByName(this.panel_idle, "CheckBox_" + j);
            var Image_selected = mo.seekWidgetByName(idleCheckButton, "Image_selected");
            var Image_bg = mo.seekWidgetByName(idleCheckButton, "Image_bg");
            Image_selected.setVisible(false);
            Image_bg.setVisible(false);
        }

        if(isMyOperating){

            var startPx = -110;
            startPx = this.setOperatingButton(myUseActionRequest, this.Button_raise, [0,2], startPx);
            startPx = this.setOperatingButton(myUseActionRequest, this.Button_call, [1], startPx);
            startPx = this.setOperatingButton(myUseActionRequest, this.Button_check, [3], startPx);
            startPx = this.setOperatingButton(myUseActionRequest, this.Button_all_in, [5], startPx);
            startPx = this.setOperatingButton(myUseActionRequest, this.Button_fold, [4], startPx);

            for(var i=0; i<myUseActionRequest.length; i++){
                var actionRequest = myUseActionRequest[i];
                var  code = actionRequest.code;

                if(code == 0){
                    //押注
                    this.isRoundOfFirst = true;

                    this.betChipMinNum = actionRequest.PayNumMin;
                    this.betChipMaxNum = actionRequest.PayNumMax;
                    this.betChipNum = this.betChipMinNum;
                }if(code == 1){
                    //跟注
                    var callNum = actionRequest.PayNumMin;
                    var Text_payNum = mo.seekWidgetByName(this.Button_call, "Text_payNum");
                    mo.gameHelper.setTextString(Text_payNum, callNum);

                    this.callChipNum = callNum;



                }if(code == 2){
                    //加注
                    this.betChipMinNum = actionRequest.PayNumMin;
                    this.betChipMaxNum = actionRequest.PayNumMax;
                    this.betChipNum = this.betChipMinNum;

                }if(code == 3){
                    //让牌

                }if(code == 4){
                    //弃牌
                    if(this.idleSelectedOpration == 4){
                        mo.gameMsgManager.sendActionRequestdzpk(4, 0, 0);
                    }
                }if(code == 5){
                    //all in

                }
            }

            //每一轮第一个人不能快捷操作，防止全都是让牌
            if(mo.dzpkTableStatus.mTableStatus.Round - mo.dzpkTableStatus.mOldTableStatus.Round == 0){
                this.qucklyOpration();
            }else{
                this.setOprationState(true);
            }

            this.setChipNum();
        }else{
            this.setOprationState(false);
        }
    },

    //快捷操作
    qucklyOpration : function () {
        if(this.idleSelectedOpration == 1 && this.Button_call.isVisible()){
            //跟任意注
            mo.gameMsgManager.sendActionRequestdzpk(1, this.callChipNum, 0);
        }else if(this.idleSelectedOpration == 2 && this.Button_check.isVisible()){
            //自动看牌
            mo.gameMsgManager.sendActionRequestdzpk(3, 0, 0);
        }else if(this.idleSelectedOpration == 3){
            //让或弃  可以让牌就让牌，否则就弃牌
            if(this.Button_check.isVisible()){
                mo.gameMsgManager.sendActionRequestdzpk(3, 0, 0);
            }else{
                mo.gameMsgManager.sendActionRequestdzpk(4, 0, 0);
            }
        }else if(this.idleSelectedOpration == 4 && this.Button_fold.isVisible()){
            //弃牌
            mo.gameMsgManager.sendActionRequestdzpk(4, 0, 0);
        }else{
            this.setOprationState(true);
        }
    },



    setOprationState : function (isMyOperating) {
        var opration_toPos = cc.p(0, 0);
        var idle_toPos = cc.p(0, 0);

        this.panel_opration.stopAllActions();
        this.panel_idle.stopAllActions();

        if(isMyOperating){
            opration_toPos = cc.p(this.panel_opration.getPositionX(), 0)
            idle_toPos = cc.p(this.panel_idle.getPositionX(), -120)
        }else{
            opration_toPos = cc.p(this.panel_opration.getPositionX(), -120)
            idle_toPos = cc.p(this.panel_idle.getPositionX(), 0)
        }

        this.panel_opration.runAction(cc.moveTo(0.2, opration_toPos));
        this.panel_idle.runAction(cc.moveTo(0.2, idle_toPos));
    },



    setOperatingButton : function(newActionList, button, codes, pox){
        var isShow = false;
        for (var i = 0; i < codes.length; i++){
            for(var j = 0; j < newActionList.length; j++){
                var code = newActionList[j].code;
                if (code == codes[i]){
                    isShow = true;
                    break;
                }
            }
        }

        if (isShow){
            button.setVisible(true);
            button.setPositionX(pox);
            pox -= 180;
        }else{
            button.setVisible(false);
        }
        return pox;
    },
});